package com.kehutong.contact.service.impl;

import com.kehutong.common.util.Token;
import com.kehutong.contact.entity.Customer;
import com.kehutong.contact.entity.CustomerRecord;
import com.kehutong.contact.service.CustomerRecordService;
import org.apache.commons.beanutils.BeanUtils;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.UUIDGenerator;

import java.util.List;
import java.util.Objects;

/**
 * 客户归属记录
 * @author liujinwang
 * description:
 */
@WebService("/admin/crm/customer/record/service")
public class CustomerRecordServiceImpl implements CustomerRecordService {

    @Inject
    private JdbcSession jdbcSession;

    @ReqMapping("/page")
    @Override
    public Object page(JSONObject jsonObject) {
        return Result.success(jdbcSession.findPage(CustomerRecord.class)
                .eq(jsonObject, "customerId")
                .eq("deleted",false)
                .order("createTime", "desc")
                .page(jsonObject)
                .exe());
    }

    @ReqMapping("/save/update")
    @Bean(copy = true)
    @Override
    public Object saveOrUpdate(Customer customer, Token token) throws Exception{
        CustomerRecord customerRecord = jdbcSession.findOne(CustomerRecord.class).eq("customerId", customer.getId()).exe();
        if (Objects.isNull(customerRecord)) {
            customerRecord = Pool.newInstance(CustomerRecord.class, UUIDGenerator.randomUUID());
            customerRecord.setCustomerId(customer.getId());
            customerRecord.setBeforeName(customer.getCustomerBelong());
            customerRecord.setBeforeNo(customer.getCustomerBelongId());
            extracted(customer, token, customerRecord);
            jdbcSession.insert(customerRecord);
        } else {
            customerRecord.setBeforeName(customerRecord.getCurrentName());
            customerRecord.setBeforeNo(customerRecord.getCurrentNo());
            extracted(customer, token, customerRecord);
            jdbcSession.updateById(customerRecord);
        }
        return com.kehutong.common.errcode.Result.success();
    }

    @Override
    public Object delete(Customer customer) {
        List<CustomerRecord> customerRecords = jdbcSession.findArray(CustomerRecord.class).eq("customerId", customer.getId()).exe();
        if (Objects.nonNull(customerRecords)) {
            customerRecords.forEach(e -> e.setDeleted(true));
            jdbcSession.updateById(customerRecords);
        }
        return Result.success();
    }

    private void extracted(Customer customer, Token token, CustomerRecord customerRecord) {
        customerRecord.setCurrentName(customer.getCustomerBelong());
        customerRecord.setCurrentNo(customer.getCustomerBelongId());
        customerRecord.setCustomerType(customer.getCustomerType());
        customerRecord.setCustomerStatus(customer.getCustomerStatus());
        customerRecord.setIndustry(customer.getIndustry());
        customerRecord.setOperateNo(token.getUuid());
        customerRecord.setOperateName(token.getUsername());
    }
}
